package san.java.day14;

import java.lang.reflect.Field;

/**
 * 反射机制调用指定属性
 */

public class Reflect_09 {

    public static void testField() throws Exception {
        Class<?> dogClass = Class.forName("san.java.day14.GoldenDog");

        GoldenDog dog = (GoldenDog) dogClass.getConstructor(String.class, int.class).newInstance("菠萝", 1);

        // 获取公有属性
        Field is = dogClass.getField("nicheng");
        is.set(dog, "100");
        System.out.println(dog.nicheng);

        // 获取私有属性
        Field privateI = dogClass.getDeclaredField("privateI");
        privateI.setAccessible(true);
        privateI.set(dog, 1000);
        System.out.println(dog.getPrivateI());

    }

    public static void main(String[] args) {
        try {
            testField();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
